﻿using Zhp.Common.Extention;
using Zhp.Entity.Chat;
using Zhp.Entity.SysEnum;
using Zhp.SqlSugar.Trigger;
using Zhp.SqlSugar.Trigger.Core;

namespace Zhp.Trigger.Service
{
    [Trigger(nameof(ChatContent))]
    public sealed class ChatContentTrigger : BaseTriggerService<ChatContent, long>
    {
        public override void Inserted(ChatContent msg)
        {
            string content = msg.Content;
            if (msg.MessageType == MessageTypeEnum.Img)
            {
                content = MessageTypeEnum.Img.GetDescription();
            }
            var chatUserList = Context.Queryable<ChatUser>().Where(x => x.PairID == msg.PairID).ToList();
            if (chatUserList.Any())
            {
                foreach (var user in chatUserList)
                {
                    user.LastMessage = content;
                    user.LastTime = DateTime.Now;
                }

                Context.Updateable(chatUserList).ExecuteCommand();
            }
        }

    }
}